package com.self.war.bootstrap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ConfigurableWebApplicationContext;

/**
 * 
 * @author kamel.mahdi
 * 
 */
@Component
public class AppInitializer implements
		ApplicationContextInitializer<ConfigurableWebApplicationContext> {
	private static Logger LOGGER = LoggerFactory
			.getLogger(AppInitializer.class);

	@Override
	public void initialize(ConfigurableWebApplicationContext configuration) {
		LOGGER.debug("starting bootstrap phase.....");
		Environment env = configuration.getEnvironment();
		if (env.getActiveProfiles() != null
				&& env.getActiveProfiles().length > 0) {
			LOGGER.info("**********************************************************************");
			LOGGER.info("***															   ****");
			LOGGER.info("***															   ****");
			LOGGER.info("***															   ****");
			LOGGER.info("***															   ****");
			String[] profiles = env.getActiveProfiles();
			LOGGER.info(
					"***	Application running with the following profile '{}'",
					env.getActiveProfiles());
			configuration.getEnvironment().setActiveProfiles(profiles);
			configuration.refresh();
			LOGGER.info("***															   ****");
			LOGGER.info("***															   ****");
			LOGGER.info("***															   ****");
			LOGGER.info("**********************************************************************");
		} else {
			LOGGER.info("**********************************************************************");
			LOGGER.info("***															   ****");
			LOGGER.info("***															   ****");
			LOGGER.info("***															   ****");
			LOGGER.info("***															   ****");
			configuration.getEnvironment().setActiveProfiles("dev");
			LOGGER.info("********  Application running dev profile						 ******");
			LOGGER.info("***															   ****");
			LOGGER.info("***															   ****");
			LOGGER.info("***															   ****");
			LOGGER.info("***															   ****");
			LOGGER.info("**********************************************************************");
		}
		initData();
		LOGGER.debug("bootstrap phase done .....");
	}

	/**
	 * utility to create a bootstraap
	 */
	public void initData() {
		LOGGER.info("**********************************************************************");
		LOGGER.info("***															   ****");
		LOGGER.info("***															   ****");
		LOGGER.info("***															   ****");
		LOGGER.info("***															   ****");
		LOGGER.debug("performing bootstrap phase.....");
		LOGGER.info("***															   ****");
		LOGGER.info("***															   ****");
		LOGGER.info("***															   ****");
		LOGGER.info("***															   ****");
		LOGGER.info("**********************************************************************");

	}
}
